package com.lnj.community.controller;

import com.lnj.community.config.OSSConfig;
import com.lnj.community.dao.entity.Event;
import com.lnj.community.event.EventProduct;
import com.lnj.community.utiles.CommunityUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.protocol.types.Field;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import static com.lnj.community.utiles.CommunityConstant.TOPIC_SHARE;

@Controller
public class ShareController {
    private static final Logger log= LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private EventProduct eventProduct; // 注入事件生产者 kafka消息队列

    @Value("${community.path.domain}")
    private String domain;
    @Value("${server.servlet.context-path}")
    private String contextPath;
    @Value("${wk.image.storage}")
    private String wkImageStorage;

    //分享
    @RequestMapping(path = "/share", method = RequestMethod.GET)
    @ResponseBody
    public String share(String htmlUrl) {
        // 生成分享图片的名称
        String fileName= CommunityUtil.generateUUID();

        //分享事件 kafka异步处理 异步生成图片
        Event event=new Event()
                .setTopic(TOPIC_SHARE)
                .setData("htmlUrl",htmlUrl)
               .setData("fileName",fileName)
               .setData("suffix",".png");
        eventProduct.fireEvent(event);

        //返回访问路径
        Map<String, Object> map=new HashMap<>();
        map.put("shareUrl","https://" + OSSConfig.BUCKET_NAME + "." + OSSConfig.ENDPOINT + "/" + fileName+".png");

        return CommunityUtil.getJSONString(0,null,map);
    }

    //获取分享图片
    @RequestMapping(path = "/share/image/{fileName}", method = RequestMethod.GET)
    public void getShareImage(@PathVariable("fileName") String fileName, HttpServletResponse response) {
        if (StringUtils.isBlank(fileName)) {
            throw new IllegalArgumentException("文件名不能为空!");
        }
        response.setContentType("image/png");
        //读取文件的后缀名
        File file=new File(wkImageStorage+"/"+fileName+".png");
        try {
            OutputStream os=response.getOutputStream();
            FileInputStream fis=new FileInputStream(file);
            byte[] buffer=new byte[4*1024];
            int len=0;
            while ((len=fis.read(buffer))!=-1) {
                os.write(buffer,0,len);
            }
        }catch (Exception e) {
            log.error("读取分享图片失败:"+e.getMessage());
        }
    }

}
